<?php
// 人员管理
namespace Admin\Controller;
use Think\Controller;
class ManageController extends Controller
{
	// 添加/修改
	public function userChange($value='')
	{
		$User=D('Manage');
		
		$role_id			=	I('post.role_id/d');
		$user_id			=	I('post.user_id/d');
		$account 			=	I('post.account/s');
		$user_name 			=	I('post.user_name/s');
		$password			=	I('post.password/s');
		
		$data['role_id']	=	$role_id;
		$data['account']	=	$account;
		$data['user_name']	=	$user_name;
		$data['password']	=	getPwdEncodeString($password);
	 

		if (!$User->create($data)) {
			$result['msg']=$User->getError();
			$result['code']=500;
			$this->ajaxReturn($result);	
		}

		if (empty($user_id)) {
			$data['add_time']=time();
			$data['account_status']=1;
			
			$res=$User->userAdd($data);
			if ($res) {
				$result['msg']='添加成功';
				$result['code']=200;		 
			}else{
				$result['msg']='添加失败';
				$result['code']=500;
			}
			
		}else{
			
			$res=$User->userSave($user_id,$data);
			if ($res) {
				$result['msg']='修改成功';
				$result['code']=200;		 
			}else{
				$result['msg']='修改失败';
				$result['code']=500;
			}
		}

		$this->ajaxReturn($result);

	}
	// 人员详情
	public function userInfo($value='')
	{
		$id=I('post.user_id/d');
		$User=D('Manage');
		$data=$User->userInfo($id);
		$result['msg']='查询成功';
		$result['code']=200;
		$result['data']=$data;
		$this->ajaxReturn($result);
		
	}
	// 
	public function userDelete($value='')
	{
		$user_id=I('post.user_id');
		$res=D('Manage')->userDelete($user_id);
		if ($res) {
			$result['msg']='删除成功';
			$result['code']=200;
		}else{
			$result['msg']='删除失败';
			$result['code']=500;
		}
		$this->ajaxReturn($result);
	}

	// 用户列表
	public function userList($value='')
	{
		$role_id	=	I('post.role_id/d');
		$start_time	=	I('post.start_time/s');
		$end_time	=	I('post.end_time/s');
		$page 		=	I('post.page/d');
		$page_size	=	I('post.page_size/d');

		$arr=$User=D('Manage')->userList($role_id,$start_time,$end_time,$page,$page_size);
		
		$result['msg']='查询成功';
		$result['code']=200;
		$result['data']['total_page']=$arr['count'];
		$result['data']['list']=$arr['data']; 

		$this->ajaxReturn($result);
	}

}